﻿using MaiPlanServer.IServices.IOrganizationaServices;
using MaiPlanServer.Model;
using MaiPlanServer.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Validations;
using System;
using System.Threading.Tasks;

namespace MaiPlanServer.API.Controllers.Organizational
{
    [Route("api/[controller]")]
    [ApiController]
    public class PostManageController : ControllerBase
    {
        public PostManageController(IPostManageServices db)
        {
            Db = db;
        }

        public IPostManageServices Db { get; }
        /// <summary>
        /// 分页获取岗位数据
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        /// <param name="PageSize">每页显示条数</param>
        /// <param name="post_code">岗位编码</param>
        /// <param name="post_name">岗位名称</param>
        /// <param name="status">岗位状态</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(int PageIndex, int PageSize, string post_code, string post_name, string status)
        {
            try
            {
                var list = await Db.GetPost(PageIndex, PageSize, post_code, post_name, status);
                var count = await Db.GetCountAsync(post_name);
                return Ok(BaseState.CreateInstance(200,"请求成功",list,count));
            }
            catch (System.Exception e)
            {
                //后续写日志
            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败ex"));
        }
        /// <summary>
        /// 新增部门
        /// </summary>
        /// <param name="model">岗位模型</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromForm]sys_post model)
        {
            try
            {
                model.post_id = Guid.NewGuid().ToString();
                model.create_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                var result = await Db.AddAsync(model);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200,"新增成功"));
                }
            }
            catch (System.Exception e)
            {
                //后续写日志
            }
            return Ok(BaseState.CreateInstance(-1, "新增失败，服务器异常"));
        }
        /// <summary>
        /// 修改岗位
        /// </summary>
        /// <param name="model">岗位模型</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Put([FromForm] sys_post model)
        {
            try
            {
                model.update_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                var result = await Db.UpdateAsync(model);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "修改成功"));
                }
            }
            catch (Exception e)
            {
                //后续写日志
            }
            return Ok(BaseState.CreateInstance(-1, "修改失败，服务器异常"));
        }
        /// <summary>
        /// 删除岗位
        /// </summary>
        /// <param name="post_id">岗位id</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Del(string post_id)
        {
            try
            {
                var result = await Db.DellAsync(post_id);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
            }
            catch (Exception e)
            {
                //后续写日志
            }
            return Ok(BaseState.CreateInstance(200, "删除失败，服务器异常"));
        }
        /// <summary>
        /// 删除多条数据
        /// </summary>
        /// <param name="post_ids"></param>
        /// <returns></returns>
        [HttpDelete("DelAll")]
        public async Task<IActionResult> DelAll(string post_ids)
        {
            try
            {
                string[] ids = post_ids.Split(',');
                var result = await Db.DellAllAsync(ids);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
            }
            catch (Exception e)
            {
                //后续写日志
            }
            return Ok(BaseState.CreateInstance(200, "删除失败，服务器异常"));
        }
    }
}
